package com.stylefeng.guns.modular.netcar.service.impl;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.model.CommonKeyValue;
import com.stylefeng.guns.core.model.Pager;
import com.stylefeng.guns.core.util.JwtTokenUtil;
import com.stylefeng.guns.modular.netcar.dao.LineRangePriceMapper;
import com.stylefeng.guns.modular.netcar.model.LineCity;
import com.stylefeng.guns.modular.netcar.model.LineRangePrice;
import com.stylefeng.guns.modular.netcar.model.OrderLine;
import com.stylefeng.guns.modular.netcar.service.ILineCityService;
import com.stylefeng.guns.modular.netcar.service.ILineRangePriceService;
import com.stylefeng.guns.modular.netcar.service.IOrderLineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;


/**

 * 线路超区域加价 Service 实现 
 * @author 张亚林
 * @Date 2018-10-27 09:54
 */
@Service
public class LineRangePriceServiceImpl extends ServiceImpl<LineRangePriceMapper,LineRangePrice> implements ILineRangePriceService {

    @Resource
    private LineRangePriceMapper lineRangePriceMapper;
    @Autowired
    private IOrderLineService orderLineService;
    @Autowired
    private ILineCityService lineCityService;
    /**
     * 删除线路超区域加价
     * @param lineRangePrice
     * @return     
     */
    @Transactional
    public int deleteLineRangePrice(LineRangePrice lineRangePrice,String token)throws Exception{
        if(lineRangePrice==null ){
            throw new RuntimeException("参数不全！");
        }
        return this.lineRangePriceMapper.deleteLineRangePrice(lineRangePrice);
    }
    /**
     * 批量删除线路超区域加价
     * @param ids 主键ID数组
     * @return 
     */
    @Transactional
    public int deleteBatchLineRangePrice(String[] ids,String token)throws Exception{
        return this.lineRangePriceMapper.deleteBatchLineRangePrice(ids);
    }
    /**
     * 查询线路超区域加价列表
     * @param lineRangePrice 线路超区域加价对象
     * @return
     */
    public List<LineRangePrice> selectListLineRangePrice(LineRangePrice lineRangePrice,String token)throws Exception{
        return  this.lineRangePriceMapper.selectListLineRangePrice(lineRangePrice);
    }
    /**
	 * 分页查询线路超区域加价列表
     * @param pager 分页对象
     * @return
     */     
    public  Map pageListLineRangePrice(Pager pager,String token) throws Exception{
        Page page=new Page(pager.getPageNumber(),pager.getPageSize());
        page.setRecords(lineRangePriceMapper.selectListLineRangePrice(page,(LineRangePrice)pager.getObj()));
        List<LineRangePrice> list = page.getRecords();
        Map mapDate = new HashMap();
        mapDate.put("rows", list);
        mapDate.put("total", page.getTotal());
        mapDate.put("page", page.getCurrent());
        mapDate.put("pageCount", page.getPages());
        return mapDate;
    }
    /**
     * 同一个事务 互斥锁查询
     * @param lineRangePrice 线路超区域加价对象
     * @return 
     */
    public  List<LineRangePrice> selectListLineRangePriceForUpdate(LineRangePrice lineRangePrice)throws Exception{
        return  this.lineRangePriceMapper.selectListLineRangePriceForUpdate(lineRangePrice);
    }
    /**
     * 通过主键id查询的线路超区域加价列表
     * @param id 主键id
     * @return     
     */
    public  LineRangePrice findLineRangePriceById(String id,String token)throws Exception{
        return  this.lineRangePriceMapper.findLineRangePriceById(id);
    }

    private void checkData(String olId,String lcId)throws Exception{
        OrderLine orderLine=orderLineService.findOrderLineById(olId,null);
        if(orderLine==null || orderLine.getOlIsAlived().equals("0")){
            throw new RuntimeException("线路不存在或者已经禁用");
        }
        if(!(orderLine.getOlStartCityId().equals(lcId) || orderLine.getOlEndCityId().equals(lcId))){
            throw new RuntimeException("城市id不在线路中！");
        }
        LineCity lc=lineCityService.findLineCityById(lcId,null);
        if(lc==null || lc.getLcIsAlived().equals("0")){
            throw new RuntimeException("城市不存在或者已经禁用");
        }
    }
    /**
     * 新增线路超区域加价数据
     * @param lineRangePrice  线路超区域加价对象
     * @return
     */
    @Transactional
    public int insertLineRangePrice(LineRangePrice lineRangePrice,String token) throws Exception {
        checkData(lineRangePrice.getOlId(),lineRangePrice.getLcId());
        if(lineRangePrice!=null && (lineRangePrice.getLrpId()==null || lineRangePrice.getLrpId().trim().length()<10)){
            lineRangePrice.setLrpId(IdWorker.get32UUID());
        }  
        return this.lineRangePriceMapper.insertLineRangePrice(lineRangePrice);
    }

    /**
     * 初始化线路超区域加价数据
     * @param olId  线路超区域加价对象
     * @param olId  线路超区域加价对象
     * @return
     */
    @Transactional
    public int insertInitLineRangePrice(String olId,String lcId,String token) throws Exception {
        checkData(olId,lcId);
        LineRangePrice lineRangePrice=new LineRangePrice();
        lineRangePrice.setOlId(olId);
        lineRangePrice.setLcId(lcId);
        lineRangePrice.setCreateTime(new Date());
        lineRangePrice.setCreateUser(JwtTokenUtil.getClaimFromToken(token).getSubject());
        return this.lineRangePriceMapper.insertInitLineRangePrice(lineRangePrice);
    }


    /**
     * 更新线路超区域加价数据
     * @param lineRangePrice 线路超区域加价对象
     * @return     
     */
    @Transactional
    public int updateLineRangePrice(LineRangePrice lineRangePrice,String token) throws Exception {
        return  this.lineRangePriceMapper.updateLineRangePrice(lineRangePrice);
    }    
    /**
     * 查询线路超区域加价返回key-Value属性
     * @param lineRangePrice 系统操作对象
     * @return     
     */
    public List<CommonKeyValue> optionsLineRangePrice(LineRangePrice lineRangePrice,String token) throws Exception {
        List<LineRangePrice> listLineRangePrice=this.lineRangePriceMapper.selectListLineRangePrice(lineRangePrice);
        List<CommonKeyValue> listOptions=new ArrayList<CommonKeyValue>();
        for(LineRangePrice item:listLineRangePrice){
            CommonKeyValue opts=new CommonKeyValue();
            opts.setId(""+item.getLrpId());
            opts.setValue(item.getOlId());
            listOptions.add(opts);
        }
        return listOptions;
    }
}
